package com.fl.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.fl.sys.SysConfig;
import com.healthmarketscience.jackcess.Cursor;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.Table;
/**
 * @author liliang
 * 
 */
public class AccessUtil {

	public static final Logger log = Logger.getLogger(AccessUtil.class);
	
	public static List<Map<String, Object>> findAll(String tableName)
			throws Exception {

		log.info("查找表" + tableName + "全表数据开始...");

		Database db = getDatabase();
		List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> row : db.getTable(tableName)) {
			rows.add(row);
		}
		close(db);

		log.info("查找表" + tableName + "全表数据成功，数据" + rows.size() + "条");
		return rows;
	}

	public static Map<String, Object> findByKey(String tableName, String key)
			throws Exception {

		Database db = getDatabase();

		log.info("查找表" + tableName + "表主键为" + key + "开始...");

		Map<String, Object> result = new HashMap<String, Object>();
		Table table = db.getTable(tableName);
		Cursor cursor = Cursor.createCursor(table);
		cursor.findFirstRow(table.getColumn(SysConfig.TALBE_COL_KEY), key);
		result = cursor.getCurrentRow();

		close(db);

		log.info("查找表" + tableName + "表主键为" + key + "，结果" + result);
		return result;
	}

	public static void addRow(String tableName, Map<String, Object> rowMap)
			throws Exception {

		Database db = getDatabase();

		log.info("表" + tableName + "表新增记录" + rowMap + "开始...");
		
		Table table = db.getTable(tableName);
		Object key = rowMap.get(SysConfig.TALBE_COL_KEY);
		if (!canAdd(table, key)) {
			throw new Exception("主键重复[" + key + "]!");
		}
		Object[] row = table.asRow(rowMap);
		table.addRow(row);

		close(db);
		log.info("表" + tableName + "表新增记录" + rowMap + "成功");
	}

	public static void addRows(String tableName,
			List<Map<String, Object>> rowMaps) throws Exception {
		for (Map<String, Object> rowMap : rowMaps) {
			addRow(tableName, rowMap);
		}
	}

	public static void updateRow(String tableName, Map<String, Object> rowMap)
			throws Exception {

		Database db = getDatabase();

		log.info("表" + tableName + "更新增记录" + rowMap + "开始...");
		
		Object keyValue = rowMap.get(SysConfig.TALBE_COL_KEY);
		Table table = db.getTable(tableName);
		Cursor cursor = Cursor.createCursor(table);
		cursor.findFirstRow(table.getColumn(SysConfig.TALBE_COL_KEY), keyValue);
		Map<String, Object> updateRowMap = cursor.getCurrentRow();

		for (String key : rowMap.keySet()) {
			if (!key.equals(SysConfig.TALBE_COL_KEY)) {
				updateRowMap.put(key, rowMap.get(key));
			}

		}
		Object[] row = table.asUpdateRow(updateRowMap);
		cursor.updateCurrentRow(row);

		close(db);
		
		log.info("表" + tableName + "更新增记录" + rowMap + "成功");
	}

	private static boolean canAdd(Table table, Object key) {

		for (Map<String, Object> row : table) {
			if (key.equals(row.get(SysConfig.TALBE_COL_KEY))) {
				return false;
			}
		}
		return true;
	}

	private static Database getDatabase() throws Exception {
		return Database.open(new File(SysConfig.ACCESS_FILE_PATH));
	}

	private static void close(Database db) throws Exception {
		db.close();
	}
}
